<template>
    <div>
        <Modal
                v-model="modelShow"
                title="人员档案交接">
            <Select v-model="selectCompany" style="width:200px">
                <Option v-for="item in companyList" :value="item.code" :key="item.code">{{ item.name }}</Option>
            </Select>
            <div slot="footer">
                <Button type="text" @click="cancelAction">取消</Button>
                <Button type="success" :loading="loading" @click="okAction">确定</Button>
            </div>
        </Modal>
        <review-modal ref="reviewModal" :table-column="failColumn" :table-data="failData"
                      :modal-show="reviewModalShow" :modal-title="reviewTitle"
                      @submitOption="submitOption()"></review-modal>
    </div>
</template>

<script>
import { updaterecordConnection, getOfficeList } from '@/api/record'
import ReviewModal from './components/review-modal'
export default {
  name: 'TurnPeopleModel',
  components: { ReviewModal },
  data () {
    return {
      modelShow: false,
      selectCompany: '',
      companyList: [],
      failColumn: [
        {
          key: 'name',
          title: '姓名'
        },
        {
          key: 'idcard',
          title: '身份证号'
        }
      ],
      failData: [],
      reviewTitle: '',
      reviewModalShow: false,
      loading: false
    }
  },
  props: {
    options: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  methods: {
    modelAction () {
      this.modelShow = !this.modelShow
      this.getOfficeList()
    },
    getOfficeList () {
      getOfficeList({ parent_id: '044f4fc3-4a38-11ea-ba01-ec0d9acf02d2' }).then(ret => {
        if (ret.data.errcode === 0) {
          this.companyList = ret.data.data
        } else {
          this.$Message.error(ret.data.errmsg)
        }
      })
    },
    submitOption () {
      this.reviewModalShow = false
    },
    okAction () {
      const data = {
        accept_company_code: this.selectCompany,
        selectlist: this.options
      }
      this.loading = true
      updaterecordConnection(data).then(ret => {
        const data = ret.data
        if (data.errcode === 0) {
          if (data.data.errList && data.data.errList.length !== 0) {
            this.reviewTitle = '以下人员数据有误请核实'
            this.failData = data.data.errList
            this.$Message.error(data.data.errMsg)
            this.selectCompany = ''
            this.modelShow = !this.modelShow
            this.loading = false
            this.reviewModalShow = true
          } else {
            this.selectCompany = ''
            this.$emit('clean')
            this.$Message.success('操作成功')
            this.modelShow = !this.modelShow
            this.loading = false
          }
        } else {
          this.$Message.error(data.errmsg)
          this.loading = false
        }
      })
    },
    cancelAction () {
      this.modelShow = false
    }
  },
  computed: {
    getUploadUrl () {
      return this.$config.upLoadFileUrl
    }
  }
}
</script>

<style scoped>

</style>
